﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PN264.Resources;

namespace PN264.Dialogs
{
    internal sealed partial class WorkerConfigurationDialogForm : Form
    {
        #region Constructors
        internal WorkerConfigurationDialogForm(WorkerConfigurationDialog dialog)
        {
            this.dialog = dialog;

            this.InitializeComponent();
            this.dialog.DefaultPresetChanged += new EventHandler(dialog_DefaultPresetChanged);
            this.dialog.DefaultWorkerCountChanged += new EventHandler(dialog_DefaultWorkerCountChanged);
            this.dialog.SelectedPresetChanged += new EventHandler(dialog_SelectedPresetChanged);
            this.dialog.SelectedWorkerCountChanged += new EventHandler(dialog_SelectedWorkerCountChanged);
        }

        void dialog_SelectedWorkerCountChanged(object sender, EventArgs e)
        {
            this.UpdateUIState();
        }

        void dialog_SelectedPresetChanged(object sender, EventArgs e)
        {
            this.UpdateUIState();
        }

        void dialog_DefaultWorkerCountChanged(object sender, EventArgs e)
        {
            this.UpdateUIState();
        }

        void dialog_DefaultPresetChanged(object sender, EventArgs e)
        {
            this.UpdateUIState();
        }
        #endregion
        #region Fields
        private WorkerConfigurationDialog dialog;
        private NumericUpDown workerCountInput;
        private RadioButton otherOption;
        private RadioButton singleOption;
        private RadioButton noneOption;
        private RadioButton cpuOption;
        private Button cancelButton;
        private Button acceptButton;
        #endregion
        #region Methods
        
        private void UpdateUIState()
        {
            var preset = this.dialog.SelectedPreset;

            if (preset == dialog.DefaultPreset)
            {
                this.acceptButton.Enabled = false;
                this.acceptButton.Text = ProgramStrings.AcceptButtonNoChanges;
                this.cancelButton.Text = ProgramStrings.CancelButtonNoChanges;
            }
            else
            {
                this.acceptButton.Enabled = true;
                this.acceptButton.Text = ProgramStrings.AcceptButton;
                this.cancelButton.Text = ProgramStrings.CancelButton;
            }
            if (preset == WorkerPoolCapacityPreset.UserConfigured)
            {
                this.workerCountInput.Enabled = true;
            }
            else
            {
                this.workerCountInput.Enabled = false;
            }
        }
        private void ChangeWorkerCount(int count)
        {

        }
        private void noneOption_CheckedChanged(object sender, EventArgs e)
        {
            this.dialog.SelectedPreset = WorkerPoolCapacityPreset.None;
        }

        private void singleOption_CheckedChanged(object sender, EventArgs e)
        {
            this.dialog.SelectedPreset = WorkerPoolCapacityPreset.Single;
        }

        private void cpuOption_CheckedChanged(object sender, EventArgs e)
        {
            this.dialog.SelectedPreset = WorkerPoolCapacityPreset.LogicalCpuCount;
        }

        private void otherOption_CheckedChanged(object sender, EventArgs e)
        {
            this.dialog.SelectedPreset = WorkerPoolCapacityPreset.UserConfigured;
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            this.ChangeWorkerCount((int)this.workerCountInput.Value);
            base.OnClosing(e);
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void acceptButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        #endregion
    }
}
